<?php

namespace Theatre\Client;

class Ticket extends \Theatre\Controller {
	function index() {
	}


	/**
	 * Request booking
	 */
	function requestBooking() {
		$this->view('JSON');

		if (empty($this->params['hall_id']) || !is_numeric($this->params['hall_id'])) {
			$this->set(array(
				'status'  => 'error',
				'message' => 'No Hall Id given'
			));
		}

		if (empty($this->params['show_id']) || !is_numeric($this->params['show_id'])) {
			$this->set(array(
				'status'  => 'error',
				'message' => 'No Show Id given'
			));
		}

		if (empty($this->params['representation_id']) || !is_numeric($this->params['representation_id'])) {
			$this->set(array(
				'status'  => 'error',
				'message' => 'No representation Id given'
			));
		}


		$result = null;

		if (empty($this->params['name'])) {
			if (empty($this->params['seats']) || $this->params['seats'] <= 0 ) {
				$this->set(array(
					'status'  => 'error',
					'message' => 'No seats number giver'
				));

				return;
			}


			$seat_exception = empty($_SESSION['seat_exception']) ? array() : $_SESSION['seat_exception'];
			$result = $this->model('TicketAPI')->getSeats(
				$this->params['hall_id'],
				$this->params['show_id'],
				$this->params['representation_id'],
				$this->params['seats'],
				$seat_exception
			);

			if (empty($_SESSION['seat_exception'])) {
				$_SESSION['seat_exception'] = array();
			}

			if (!empty($result) && !empty($result['seats']) && is_array($result['seats'])) {
				$_SESSION['seat_exception'] = array_merge($_SESSION['seat_exception'], $result['seats']);
				$_SESSION['seat_exception'] = array_unique($_SESSION['seat_exception']);
			}

			if (!empty($result) && empty($result['seats'])) {
				unset($_SESSION['seat_exception']);
			}
		}
		else {
			if (!empty($_SESSION['seat_exception'])) {
				unset($_SESSION['seat_exception']);
			}

			$result = $this->model('TicketAPI')->bookSeats(
				$this->params['hall_id'],
				$this->params['show_id'],
				$this->params['representation_id'],
				$this->params['name']);

		}

		if (!empty($result)) {
			$this->set($result);

			return;
		}

		$this->set(array(
			'status'  => 'error',
			'message' => 'Invalid Response'
		));
	}
}
